package dahuiji.common.util;

import java.io.File;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;

import org.apache.log4j.Logger;

/**
 * 解析文件路径
 * 
 * @author DELL
 * 
 */
public class UrlUtil {
	/**
	 * 日志
	 */
	private static Logger logger = Logger.getLogger(UrlUtil.class);
	private static List<String> MACHINE_LIST = new Vector<String>();
	private static String EXTENT_PATH = null;
	public static String UPLOAD_FOLDER = null;
	public static String IMAGE_PATH = null;
	public static String TEMP_IMAGE_PATH = null;
	public static String TEMP_UPLOAD_FOLDER = null;
	static {
		String filePath = null;
		try {
			Properties properties = new Properties();
			/** 加载属性文件 */
			filePath = UrlUtil.class.getResource("/").getPath()
					+ "/path.properties";
			File file = new File(filePath);
			properties.load(new FileInputStream(file));
			/** 从属性文件中获取文件存放路径 */
			EXTENT_PATH = properties.getProperty("extent_path");
			
			logger.info("EXTENT_PATH:" + EXTENT_PATH);
			
			UPLOAD_FOLDER = properties.getProperty("upload_folder");
			logger.info("UPLOAD_FOLDER:" + UPLOAD_FOLDER);
            java.io.File imageFolder = new java.io.File(UPLOAD_FOLDER); 
            if(!imageFolder.exists()){
                imageFolder.mkdir();
             } 
            
			IMAGE_PATH = properties.getProperty("image_path");
	        logger.info("IMAGE_PATH:" + IMAGE_PATH);
	        
	        TEMP_UPLOAD_FOLDER = properties.getProperty("temp_upload_folder");
            logger.info("TEMP_UPLOAD_FOLDER:" + TEMP_UPLOAD_FOLDER);
            java.io.File tempFolder = new java.io.File(TEMP_UPLOAD_FOLDER); 
            if(!tempFolder.exists()){
                 tempFolder.mkdir();
             } 
	        
            TEMP_IMAGE_PATH = properties.getProperty("temp_image_path");
            logger.info("TEMP_IMAGE_PATH:" + TEMP_IMAGE_PATH);
            
			String machines = properties.getProperty("machine_list");
			logger.info("MACHINE_LIST:" + machines);
			
			
			
			StringTokenizer st = new StringTokenizer(machines, ";");

			while (st.hasMoreElements()) {
				MACHINE_LIST.add(st.nextToken());
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/** 获取路径 **/
	public static String getRandomUrl() {
		return EXTENT_PATH.replace("$$", MACHINE_LIST.get(getRandom(0, MACHINE_LIST.size() - 1)));
	}

	public static int getRandom(int min, int max) {
		return (int) Math.round(Math.random() * (max - min) + min);
	}

	public static void main(String[] args) {
		for (int i = 0; i < 100; i++)
			System.out.println(getRandom(1,100));
	}

}
